package com.dk.hi.gui.comp;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import com.dk.hi.core.InputDTO;
import com.dk.hi.gui.Message;

public abstract class GraphicComponent {
	protected Integer width; // component width
	protected Integer height; // component height
	protected Integer compX; // component X position
	protected Integer compY; // component Y position
	private GraphicContainer container; // the component container

	// Renders the component
	public void render(Graphics g) throws SlickException {
		// Call of the render method
		renderComponent(g);
	}

	// Renders the current component
	protected abstract void renderComponent(Graphics g) throws SlickException;

	public void sendToContainer(Message msg) {
		this.container.receiveMessage(this, msg);
	}

	// Initializes the component container
	public void setContainer(GraphicContainer container) {
		this.container = container;
	}

	// Initializes the component position
	public void setPosition(Integer x, Integer y) {
		this.compX = x;
		this.compY = y;
	}

	public void setSize(int width, int height) {
		this.width = width;
		this.height = height;
	}

	// Lunches the update process for the component and it's children
	public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
		// Call of the update method overrided in subclass
		updateComponent(container, game, delta);
	}

	// Updates the current component
	protected abstract void updateComponent(GameContainer container, StateBasedGame game, int delta) throws SlickException;

	protected InputDTO buildInputDTO(GameContainer container) {
		Input input = container.getInput();
		InputDTO dto = new InputDTO();

		dto.setUp(input.isKeyDown(Input.KEY_UP));
		dto.setDown(input.isKeyDown(Input.KEY_DOWN));
		dto.setLeft(input.isKeyDown(Input.KEY_LEFT));
		dto.setRight(input.isKeyDown(Input.KEY_RIGHT));
		dto.setLeftClick(input.isMousePressed(Input.MOUSE_LEFT_BUTTON));
		dto.setRightClick(input.isMousePressed(Input.MOUSE_RIGHT_BUTTON));
		dto.setMouseX(input.getMouseX());
		dto.setMouseY(input.getMouseY());

		return dto;
	}

}
